#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 数字绑定
    connect(ui->Num0,&QPushButton::clicked,[this](){ OnClicKed(Num,"0"); });
    connect(ui->Num1,&QPushButton::clicked,[this](){ OnClicKed(Num,"1"); });
    connect(ui->Num2,&QPushButton::clicked,[this](){ OnClicKed(Num,"2"); });
    connect(ui->Num3,&QPushButton::clicked,[this](){ OnClicKed(Num,"3"); });
    connect(ui->Num4,&QPushButton::clicked,[this](){ OnClicKed(Num,"4"); });
    connect(ui->Num5,&QPushButton::clicked,[this](){ OnClicKed(Num,"5"); });
    connect(ui->Num6,&QPushButton::clicked,[this](){ OnClicKed(Num,"6"); });
    connect(ui->Num7,&QPushButton::clicked,[this](){ OnClicKed(Num,"7"); });
    connect(ui->Num8,&QPushButton::clicked,[this](){ OnClicKed(Num,"8"); });
    connect(ui->Num9,&QPushButton::clicked,[this](){ OnClicKed(Num,"9"); });

    // 运算符绑定
    connect(ui->add,&QPushButton::clicked,[this](){ OnClicKed(Op,"+"); });
    connect(ui->sub,&QPushButton::clicked,[this](){ OnClicKed(Op,"-"); });
    connect(ui->mul,&QPushButton::clicked,[this](){ OnClicKed(Op,"*"); });
    connect(ui->div,&QPushButton::clicked,[this](){ OnClicKed(Op,"/"); });

    // 其他
    connect(ui->back,&QPushButton::clicked,[this](){ OnClicKed(Back,"Back"); });
    connect(ui->clear,&QPushButton::clicked,[this](){ OnClicKed(Clear,"Clear"); });
    connect(ui->dot,&QPushButton::clicked,[this](){ OnClicKed(Dot,"."); });
    connect(ui->equal,&QPushButton::clicked,[this](){ OnClicKed(Equal,"="); });


}

MainWindow::~MainWindow()
{
    delete ui;
}

void Final_Value(QLineEdit *lineEdit, QString& mNum1, QString& mNum2, QString& mOp)
{
    double num1 = mNum1.toDouble();
    double num2 = mNum2.toDouble();
    double result = 0.0;

    if(mOp == "+") result = num1 + num2;
    else if(mOp == "-") result = num1 - num2;
    else if(mOp == "*") result = num1 * num2;
    else if(mOp == "/")
    {
        if(num2 == 0.0)
        {
            lineEdit->setText("Error!");
            return;
        }
        else
        {
            result = num1 / num2;
        }
    }

    // 输出运算结果
    lineEdit->setText(QString::number(result));

    // 改变值:
    mNum1 = QString::number(result);
    mNum2.clear();

}

void MainWindow::OnClicKed(BtnType _type, QString _btn)
{
    switch (_type)
    {
    case Num:
    {
        if(mOp.isEmpty())
        {
            mNum1 += _btn;
        }
        else
        {
            mNum2 += _btn;
        }
        break;
    }

    case Op: // 运算符
    {
        if(!mNum1.isEmpty() && !mNum2.isEmpty())
        {
            Final_Value(ui->lineEdit, mNum1, mNum2, mOp);
        }


        mOp = _btn;
        break;
    }

    case Clear:
    {
        mNum1.clear();
        mNum2.clear();
        mOp.clear();
        break;
    }

    case Dot: // 点符号
    {
        if(mOp.isEmpty())
        {
            if(!mNum1.isEmpty() && !mNum1.contains("."))
            {
                mNum1 += _btn;
            }
        }
        else
        {
            if(!mNum2.isEmpty() && !mNum2.contains("."))
            {
                mNum2 += _btn;
            }
        }
        break;
    }

    case Equal: // 运算
    {
        if(mNum1.isEmpty() || mNum2.isEmpty() || mOp.isEmpty()) return;

        Final_Value(ui->lineEdit, mNum1, mNum2, mOp); // 处理运算

        mOp.clear();
        return;
    }

    case Back: // 退格
    {
        if(!mNum1.isEmpty() && !mOp.isEmpty() && !mNum2.isEmpty())
        {
            mNum2.chop(1); // 尾部删除1;
        }
        else if(!mNum1.isEmpty() && !mOp.isEmpty())
        {
            mOp.clear();
        }
        else if(!mNum1.isEmpty())
        {
            mNum1.chop(1);
        }

        break;
    }

    }
    ui->lineEdit->setText(mNum1 + mOp + mNum2);
}
